<!DOCTYPE HTML>
<html lang="en">
<head>
<title>FileExist() - Syntax &amp; Usage | AutoHotkey</title>
<meta name="description" content="The FileExist function checks for the existence of a file or folder and returns its attributes." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>FileExist()</h1>

<p>Checks for the existence of a file or folder and returns its attributes.</p>

<pre class="Syntax">AttributeString := <span class="func">FileExist</span>(FilePattern)</pre>
<h2>Parameters</h2>
<dl>

  <dt>FilePattern</dt>
  <dd><p>The path, filename, or file pattern to check. <em>FilePattern</em> is assumed to be in <a href="../Variables.htm#WorkingDir">%A_WorkingDir%</a> if an absolute path isn't specified.</p></dd>

</dl>

<h2>Return Value</h2>
<p>This function returns the attribute string (a subset of "RASHNDOCT") of the first matching file or folder:</p>
<ul>
    <li>R = READONLY</li>
    <li>A = ARCHIVE</li>
    <li>S = SYSTEM</li>
    <li>H = HIDDEN</li>
    <li>N = NORMAL</li>
    <li>D = DIRECTORY</li>
    <li>O = OFFLINE</li>
    <li>C = COMPRESSED</li>
    <li>T = TEMPORARY</li>
</ul>
<p>If the file has no attributes (rare), "X" is returned. If no file is found, an empty string is returned.</p>

<h2>Remarks</h2>
<p>This function is a combination of <a href="IfExist.htm">IfExist</a> and <a href="FileGetAttrib.htm">FileGetAttrib</a>.</p>
<p>Since an empty string is seen as &quot;false&quot;, the function's return value can always be used as a quasi-boolean value. For example, the statement <code>if FileExist(&quot;C:\My File.txt&quot;)</code> would be true if the file exists and false otherwise. Similarly, the statement <code>if InStr(FileExist(&quot;C:\My Folder&quot;), &quot;D&quot;)</code> would be true only if the file exists <em>and</em> is a directory.</p>
<p>Since <em>FilePattern</em> may contain wildcards, FileExist may be unsuitable for validating a file path which is to be used with another function or program. For example, <code>FileExist("*.txt")</code> may return attributes even though "*.txt" is not a valid filename. In such cases, <a href="FileGetAttrib.htm">FileGetAttrib</a> is preferred.</p>

<h2>Related</h2>
<p><a href="IfExist.htm">IfExist</a>, <a href="FileGetAttrib.htm">FileGetAttrib</a>, <a href="Block.htm">Blocks</a>, <a href="Else.htm">Else</a>, <a href="LoopFile.htm">File-loops</a></p>
<h2>Examples</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>if FileExist("D:\")
    MsgBox, The drive exists.
if FileExist("D:\Docs\*.txt")
    MsgBox, At least one .txt file exists.
if !FileExist("C:\Temp\FlagFile.txt")
    MsgBox, The target file does not exist.</pre>
</div>

<div class="ex" id="ExAttr">
<p><a href="#ExAttr">#2</a>: The following example shows how to check the file for a particular attribute:</p>
<pre>if InStr(FileExist("C:\My File.txt"), "H")
    MsgBox The file is hidden.</pre>
</div>

</body>
</html>
